<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="活动标题" prop="title">
          <el-input v-model="queryParams.title" placeholder="请输入活动标题" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="活动状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="选择活动状态" clearable style="width: 200px">
            <el-option label="即将举行" :value="0" />
            <el-option label="进行中" :value="1" />
            <el-option label="已完成" :value="2" />
            <el-option label="已取消" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd">新增活动</el-button>
        </el-col>
      </el-row>
  
      <el-table v-if="refreshTable" v-loading="loading" :data="activityList" row-key="id">
        <el-table-column prop="title" label="活动标题" width="200"></el-table-column>
        <el-table-column prop="activityDate" label="活动日期" width="200"></el-table-column>
        <el-table-column prop="location" label="活动地点" width="150"></el-table-column>
        <el-table-column prop="organizer" label="活动组织者" width="150"></el-table-column>
        <el-table-column prop="contactInfo" label="联系方式" width="150"></el-table-column>
        <el-table-column prop="status" label="活动状态" width="150">
          <template #default="scope">
            <span>{{ getStatusLabel(scope.row.status) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <el-dialog :title="title" v-model="open" width="600px" append-to-body>
        <el-form ref="activityRef" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="活动标题" prop="title">
                <el-input v-model="form.title" placeholder="请输入活动标题" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="活动地点" prop="location">
                <el-input v-model="form.location" placeholder="请输入活动地点" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="活动组织者" prop="organizer">
                <el-input v-model="form.organizer" placeholder="请输入活动组织者" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="contactInfo">
                <el-input v-model="form.contactInfo" placeholder="请输入联系方式" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="活动日期" prop="activityDate">
                <el-date-picker v-model="form.activityDate" type="datetime" placeholder="选择活动日期" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="报名链接" prop="registrationLink">
                <el-input v-model="form.registrationLink" placeholder="请输入报名链接" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="活动状态" prop="status">
                <el-select v-model="form.status" placeholder="选择活动状态">
                  <el-option label="即将举行" :value="0" />
                  <el-option label="进行中" :value="1" />
                  <el-option label="已完成" :value="2" />
                  <el-option label="已取消" :value="3" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="关联设施" prop="storeId">
              <el-select v-model="form.facilityId" placeholder="请选择设施">
                <el-option v-for="facility in factilitiesList" :key="facility.id" :label="facility.name" :value="facility.id" />
              </el-select>
            </el-form-item>
          </el-col>
            <el-col :span="24">
              <el-form-item label="活动描述" prop="description">
                <el-input type="textarea" v-model="form.description" placeholder="请输入活动描述" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="ActivityList">
  import { ref, reactive } from 'vue';
  import { listActivities, addActivity, updateActivity, deleteActivity } from "@/api/pages/activity";
  import { listFacilities } from "@/api/pages/facility";
  
  const loading = ref(true);
  const open = ref(false);
  const showSearch = ref(true);
  const title = ref("");
  const refreshTable = ref(true);
  const activityList = ref([]);
  const factilitiesList = ref([]);
  const form = reactive({
    id: undefined,
    title: "",
    description: "",
    activityDate: new Date(),
    location: "",
    organizer: "",
    contactInfo: "",
    registrationLink: "",
    status: 0,
    facilityId: undefined
  });
  const queryParams = reactive({
    title: "",
    status: undefined
  });
  const rules = {
    title: [{ required: true, message: "活动标题不能为空", trigger: "blur" }],
    activityDate: [{ required: true, message: "活动日期不能为空", trigger: "blur" }],
  };
  
  // 查询活动列表
  const getList = () => {
    loading.value = true;
    listActivities(queryParams).then(response => {
      activityList.value = response.data;
      loading.value = false;
    });
  };

  // 获取设施列表
const getFacilities = () => {
  listFacilities().then(response => {
    factilitiesList.value = response.data;
  }).catch(error => {
    console.error("获取设施列表失败:", error);
  });
};
  
  // 取消按钮
  const cancel = () => {
    open.value = false;
    reset();
  };
  
  // 重置表单
  const reset = () => {
    Object.assign(form, {
      id: undefined,
      title: "",
      description: "",
      activityDate: new Date(),
      location: "",
      organizer: "",
      contactInfo: "",
      registrationLink: "",
      status: 0,
      facilityId: undefined
      
    });
  };
  
  // 搜索按钮操作
  const handleQuery = () => {
    getList();
  };
  
  // 重置按钮操作
  const resetQuery = () => {
    Object.assign(queryParams, {
      title: "",
      status: undefined
    });
    getList();
  };
  
  // 新增按钮操作
  const handleAdd = () => {
    reset();
    open.value = true;
    getFacilities();
    title.value = "新增活动";
  };
  
  // 修改按钮操作
  const handleUpdate = (row) => {
    Object.assign(form, row);
    open.value = true;
    title.value = "修改活动";
  };
  
  // 提交按钮
  const submitForm = () => {
    const apiCall = form.id ? updateActivity : addActivity;
    apiCall(form).then(() => {
      getList();
      open.value = false;
    });
  };
  
  // 删除按钮操作
  const handleDelete = (row) => {
    deleteActivity(row.id).then(() => {
      getList();
    });
  };
  
  // 获取活动状态标签
  const getStatusLabel = (status) => {
    switch (status) {
      case 0: return '即将举行';
      case 1: return '进行中';
      case 2: return '已完成';
      case 3: return '已取消';
      default: return '';
    }
  };
  
  // 初始化活动列表
  getList();
  </script>
  
  <style scoped>
  .app-container {
    padding: 20px;
  }
  .mb8 {
    margin-bottom: 8px;
  }
  .dialog-footer {
    text-align: right;
  }
  </style>